package logic

import (
	"context"
	"fmt"
	"good_srv/good"
	"good_srv/internal/svc"
	"good_srv/models"
	"good_srv/pkg"

	"github.com/zeromicro/go-zero/core/logx"
)

type AuthRealNameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAuthRealNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AuthRealNameLogic {
	return &AuthRealNameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 实名认证
func (l *AuthRealNameLogic) AuthRealName(in *good.AuthRealNameRequest) (*good.AuthRealNameResponse, error) {
	// todo: add your logic here and delete this line
	//判断用户是否存在
	var err error
	auths := models.AuthRelName{
		UserId:   in.UserId,
		IdCard:   in.IdCard,
		RealName: in.RealName,
	}
	if err = auths.AuthList(); err != nil {
		logx.Error("用户不存在")
		return nil, fmt.Errorf("用户不存在")
	}
	if auths.ID > 0 {
		logx.Error("用户已实名验证")
		return nil, fmt.Errorf("用户已实名验证")
	}
	//第三方接口
	if !pkg.AuthRealName(in.IdCard, in.RealName) {
		logx.Error("第三方接口调用失败")
		return nil, fmt.Errorf("第三方接口调用失败")
	}
	//创建实名认证
	if err = auths.AuthAdd(); err != nil {
		logx.Error("用户添加失败")
		return nil, fmt.Errorf("用户添加失败")
	}
	return &good.AuthRealNameResponse{}, nil
}
